<template>
  <div id="detail">
    
    
    <detail-nav-bar class="detail-nav" @titleClick='titleClick' ref = "nav"/>
    <scroll class="content" ref="scroll" @scroll="contentScroll" :probe-type="3">
      <!-- <ul>
        <li v-for="item in $store.state.cartList" :key="item">{{item}}</li>
      </ul> -->
      <detail-swiper :top-images="topImages"/>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <!-- <detail-shop-info :shop="shop"/> -->
      <!-- <detail-goods-info :detail-info="detailInfo"/> -->
      <detail-goods-info :detail-info = "detailInfo" @imageLoad="imageLoad"></detail-goods-info>
      <detail-param-info ref="params" :param-info = 'paramInfo'></detail-param-info>
      <detail-comment-info ref="comment" :comment-info= "commentInfo"></detail-comment-info>
      <goods-list ref="recommend" :goods= "showGoods"></goods-list>
    </scroll> 
    <detail-bottom-bar @addCart="addToCart"></detail-bottom-bar>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
    <!-- <toast class="toast" :message="message" :show="show"></toast> -->
  </div>
</template>

<script>
import DetailNavBar from './childComps/DetailNavBar'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo'
import DetailGoodsInfo from './childComps/DetailGoodsInfo'
import DetailParamInfo from './childComps/DetailParamInfo'
import DetailCommentInfo from './childComps/DetailCommentInfo'
import DetailBottomBar from './childComps/DetailBottomBar'

import Scroll from 'components/common/scroll/Scroll'
import GoodsList from 'components/content/goods/GoodsList'
// import Toast from 'components/content/toast/Toast'

import {debounce} from 'common/utils'
import {itemListenerMixin,backTopMixin} from "common/mixin"
import {getDetail,Goods,Shop,GoodsParam,getRecommend} from 'network/detail'

import {mapActions} from 'vuex'

export default {
  name:"Detail",
  components:{
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar,
    GoodsList,
    Scroll,
    // Toast
  },
  mixins:[itemListenerMixin,backTopMixin],
  data(){
    return{
      iid:null,
      topImages:[ ],
      goods:{},
      shop:{},
      detailInfo:{},
      paramInfo:{},
      commentInfo:{},
      recommends:[],
      themeTopYs:[],
      getThemeTopY : null,
      currentIndex:0,
      // message:'',
      // show:false
      
      // itemImgListener:null
    }
  },
  methods:{
    ...mapActions(['addCart']),
     imageLoad(){
    //   // this.$refs.scroll.scroll.scrollerHeight == 10000
      this.$refs.scroll.refresh()
      
    //   // console.log(this.$refs.scroll.scroll.scrollerHeight)
      this.getThemeTopY()
     },
    titleClick(index){
      // console.log(index)
      // switch(index){
      //   case 0:this.$refs.scroll.sc
      // }
      
      this.$refs.scroll.scrollTo(0,-this.themeTopYs[index],100)
      console.log()
    },
    contentScroll(position){
      const positionY = -position.y
      // for(let i in this.themeTopYs){
      //   console.log(i)
      // }
      let length = this.themeTopYs.length
      for(let i = 0 ; i< length;i++){
        // if(positionY > this.themeTopYs[i] && positionY < this.themeTopYs[i+1]){
        // }
        // if(this.currentIndex !== i && ((i<length - 1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i+1])||(i === length-1&&positionY>=this.themeTopYs[i]))){
          
        // }
        if(this.currentIndex !== i && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i+1])){
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex
        }
      }
      this.ListenShowBackTop(position)
    },
    addToCart(){
      const product = {}
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice
      product.iid = this.goods.iid
      // this.$store.dispatch('addCart',product).then(res=>{
      //   console.log(res)
      // })
      this.addCart(product).then(res=>{
        this.$toast.show(res,2000)
        // this.show = true
        // this.message = res
        // console.log(res)
        // setTimeout(()=>{
        //   this.show = false
        //   this.message = ''
        // },1500)
        
      })
    }
    
  },
  created(){
    this.iid = this.$route.params.iid
    getDetail(this.iid).then(res => {
      const data = res.result
      this.topImages = res.result.itemInfo.topImages
      this.goods = new Goods(data.itemInfo,data.columns,data.shopInfo.services)
      this.shop = new Shop(data.shopInfo)
      this.detailInfo = data.detailInfo
      this.paramInfo = new GoodsParam(data.itemParams.info,data.itemParams.rule)
      if(data.rate.cRate !== 0){
        this.commentInfo = data.rate.list[0]
      }
      this.getThemeTopY = debounce(()=>{
        this.themeTopYs = []
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.params.$el.offsetTop-44)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop-44)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop-44)
        this.themeTopYs.push(Number.MAX_VALUE)
        
      })
      // this.$nextTick(()=>{
        
      //   this.themeTopYs.push(0)
      //   this.themeTopYs.push(this.$refs.params.$el.offsetTop)
      //   this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      //   this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
      //   console.log(this.themeTopYs)
      // })
    }),
    getRecommend().then(res => {
      // console.log(res)
      this.recommends = res.data.list
    })

  },
  //updated(){
  //  },
  // mounted(){
  //   const refresh = debounce(this.$refs.scroll.refresh)

  //   this.itemImgListener = () =>{
  //     refresh()
  //   }
  //   this.$bus.$on('itemImageLoad',this.itemImgListener)
  
  // },

  destroyed(){
    this.$bus.$off('itemImageLoad',this.itemImgListener)
  },

  computed:{
    showGoods(){
      return this.recommends
    }
  }
}
</script>

<style scoped>
  #detail{
    position: relative;
    z-index: 9;
    background-color: #fff;
    height: 100vh;
  }
  .content{
    
    height: calc(100% - 102px); 
    background-color: #fff;
  }
  .detail-nav{
    position: relative;
    z-index: 9;
    background-color: rgb(191, 157, 164);
  }
  
  /* .detail-bottom{
    
  } */
</style>